package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.dict.entity.SysDictTypeVO;
import cn.xenosp.server.module.role.entity.SysRoleVO;
import cn.xenosp.server.module.role.service.SysRoleService;
import cn.xenosp.server.module.roleMenu.entity.SysRoleMenu;
import cn.xenosp.server.module.roleMenu.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-12-12 09:47
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/role/")
public class RoleController {

    @Autowired
    SysRoleService roleService;
    @Autowired
    SysRoleMenuService roleMenuService;


    @LogInfo(operationModuleType = "角色模块",operationName = "分页查询",operationContent = "查询角色数据")
    @PostMapping("/getPage")
    public Result<?> getTypePage(@RequestBody String body){
        try {
            SysRoleVO sysRoleVO = ObjectConvert.urlStringToClass(body, SysRoleVO.class);
            return Result.OK("",roleService.getPage(sysRoleVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @LogInfo(operationModuleType = "角色模块",operationName = "更新角色",operationContent = "更新角色数据")
    @PostMapping("/saveRole")
    public Result<?> saveRole(@RequestBody SysRoleVO sysRoleVO){
        try {
            roleService.update(sysRoleVO);
            return Result.OK("保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("保存数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "角色模块",operationName = "新增角色",operationContent = "新增角色数据")
    @PostMapping("/addRole")
    public Result<?> addRole(@RequestBody SysRoleVO sysRoleVO){
        try {
            roleService.save(sysRoleVO);
            return Result.OK("添加成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("添加数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "角色模块",operationName = "删除角色",operationContent = "删除角色数据")
    @PostMapping("/delRole")
    public Result<?> delRole(@RequestBody SysRoleVO sysRoleVO){
        try {
            roleService.delete(sysRoleVO);
            return Result.OK("删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("删除数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "角色模块",operationName = "角色绑定资源",operationContent = "角色绑定资源")
    @PostMapping("/bindResources")
    public Result<?> bindResources(@RequestBody SysRoleVO sysRoleVO){
        try {
            roleMenuService.roleBindResources(sysRoleVO.getId(),sysRoleVO.getMenuList());
            return Result.OK("分配成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败",null);
        }
    }
    @LogInfo(operationModuleType = "角色模块",operationName = "获取资源",operationContent = "获取角色绑定资源")
    @PostMapping("/getResources")
    public Result<?> getResources(@RequestBody SysRoleVO sysRoleVO){
        try {
            List<SysRoleMenu> roleMenuList = roleMenuService.roleGetResources(sysRoleVO.getId());
            return Result.OK("获取成功",roleMenuList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("操作失败",null);
        }
    }
}
